<?php
/**
 * Created by PhpStorm.
 * User: wangan
 * Date: 2016/12/5
 * Time: 15:44
 */

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Service\PermissionService;
use Illuminate\Http\Request;
use Cache;

class PermissionController extends Controller
{
    public function index(Request $request){
        $request->has('pageSize')?$pageSize=$request->input('pageSize'):$pageSize=20;
        $request->has('searchVal')?$searchVal=$request->input('searchVal'):$searchVal=null;
        $request->has('cid')?$cid=$request->input('cid'):$cid=0;
        $permissionList=PermissionService::getPermissionListPageInit($pageSize,['cid'=>$cid],$searchVal);
        return view('admin.permission.index',[
            'pageSize'=>$pageSize,
            'searchVal'=>$searchVal,
            'cid'=>$cid,
            'permissions'=>$permissionList,
        ]);
    }

    public function create(Request $request){
        $request->has('cid')?$cid=$request->input('cid'):$cid=0;
        return view('admin.permission.create',[
            'cid'=>$cid
        ]);
    }

    public function doCreate(Request $request){
        $this->permissionValidate($request);
        $data = $request->input('info');
        if(PermissionService::createOnePermission($data)){
            return redirect('/admin/permission/index?cid='.$data['cid']);
        }else{
            return redirect()->back();
        }
    }

    public function edit(Request $request){
        $permissionId=$request->input('pmId');
        $info = PermissionService::getOnePermission($permissionId);
        return view('admin.permission.edit',[
            'info'=>$info,
        ]);
    }

    public function doEdit(Request $request){
        $this->permissionValidate($request);
        $data = $request->input('info');
        if(PermissionService::updateOnePermission($data)){
            return redirect('/admin/permission/index?cid='.$data['cid']);
        }else{
            return redirect()->back();
        }
    }

    public function deleteOnePermission(Request $request){
        $permissionId=$request->input('permissionId');
        if(PermissionService::checkHasChild($permissionId)){
             return response()->json([
                 'code' => 417,
                 'message' => '请先删除该权限所有子菜单！'
             ]);
        }
        if(PermissionService::deletePermissions($permissionId)){
            return response()->json([
                'code' => 200,
                'message' => '操作成功！'
            ]);
        }
        else{
            return response()->json([
                'code' => 500,
                'message' => '操作失败！请重试!'
            ]);
        }
    }

    private function permissionValidate(Request $request){
        return $this->validate($request,[
            'info.name'=>'required',
            'info.label'=>'required',
        ],[
            'required'=>':attribute不能为空',
            'integer'=>':attrbute必须为整数'
        ],[
            'info.name'=>'权限规则',
            'info.label'=>'权限名称',
            'info.display_name'=>'权限名称',
            'info.description'=>'权限描述',
        ]);

    }
}